package reduce.fz.com.reduce.activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.PkGroup;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.Video;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.event.PkGroupCreateEvent;
import reduce.fz.com.reduce.event.ShowPubVideoEvent;
import reduce.fz.com.reduce.service.PkService;
import reduce.fz.com.reduce.service.VideoService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.QuPaiOper;
import reduce.fz.com.reduce.util.ServiceBuilder;

public class PkGroupCreateActivity extends BaseToolBarActivity {

    //输入框
    private EditText mEtComment;
    //保存按钮
    private TextView tvSave;
    //视频卡片
    private VideoCard videoCard;
    //趣拍sdk
    private QuPaiOper quPaiOper;
    //pk小组
    private PkGroup pkGroup = new PkGroup();
    //视频服务
    private VideoService videoService;
    //夹在弹出框
    private LoadingDialog dialog = null;
    //pk api
    private PkService pkService = ServiceBuilder.builderPkService(null);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pk_group_create);
        videoCard = (VideoCard) getIntent().getSerializableExtra("videocard");
        setToolbarTitle("编辑内容");
        tvSave = setRightBtn("保存");
        initWidget();
        quPaiOper = QuPaiOper.instance(this, true);
        videoService = ServiceBuilder.builderVideoService(null);
    }

    @Override
    public void initWidget() {
        mEtComment = (EditText) findViewById(R.id.et_comment);
        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                videoCard.description = mEtComment.getText().toString();
                subVideo();

            }
        });
    }

    /**
     * 提交pk小组
     *
     * @param pkGroup
     */
    public void submitPkGroup(PkGroup pkGroup) {
        if (dialog == null) {
            dialog = new LoadingDialog(this, "提交信息中");
            dialog.show();
        }
        pkService.pkGroupCreate(pkGroup, new AbResponseListener<PkGroup>(new TypeReference<Result<PkGroup>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                if (dialog != null)
                    dialog.dismiss();
            }

            @Override
            public void responseInfo(Result<PkGroup> data) {
                videoCard.pid = data.getData().pid;
                submitVideo(videoCard);
            }
        });
    }


    /**
     * 将视频提交到oss
     */
    public void subVideo() {
        if (dialog == null) {
            dialog = new LoadingDialog(this, "提交中");
            dialog.show();
        }
        quPaiOper.submitVideo(videoCard.video, new QuPaiOper.QuPaiListener() {
            @Override
            public void submitSuccess(Video video) {
                videoCard.video = video;
                pkGroup.description = videoCard.description;
                submitPkGroup(pkGroup);
            }

            @Override
            public void submitFailed(Video video) {

            }
        });
    }


    /**
     * 提交视频
     *
     * @param card
     */
    public void submitVideo(final VideoCard card) {
        if (dialog == null)
            dialog.setMsg("提交信息中");
        videoService.submitVideo(card, new AbResponseListener<VideoCard>(new TypeReference<Result<VideoCard>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();
            }

            @Override
            public void responseInfo(Result<VideoCard> data) {
                dialog.dismiss();
                VideoCard card = data.getData();
                card.user = UserPreference.getUser();
                pkGroup.cnum = 1l;
               // pkGroup.user = UserPreference.getUser();
                EventBus.getDefault().post(new ShowPubVideoEvent(card));
                EventBus.getDefault().post(new PkGroupCreateEvent(pkGroup));
                finish();
            }
        });
    }


    @Override
    public void initListener() {

    }
}
